home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
12984
/
12984.xpi
/
chrome
/
VideoDownloaderToolbar.jar
/
content
/
utils.js
< prev
next >
Wrap
Text File
|
2010-01-29
|
11KB
|
407 lines
if(!com) var com={};
if(!com.VidBar) com.VidBar={};
com.VidBar.__d = function(msg) {
dump("Vid debug\t" + msg + "\n");
};
com.VidBar.__e = function(msg) {
dump("Vid error\t" + msg + "\n");
};
// TODO: rethink naming of com.VidBar.Pref and Vidbar.Preferences to avoid confusion
com.VidBar.Pref = {
//VidNS = "http://viddownloader.com/1.0#",
observers : {},
pref : Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService).getBranch("vidbar."),
prefBranch2 : null,
init : function() {
com.VidBar.__d("Pref: init");
this.prefBranch2 = this.pref
.QueryInterface(Components.interfaces.nsIPrefBranch2);
this.prefBranch2.addObserver("", this, false);
},
close : function(event) {
com.VidBar.__d("Pref: close");
try {
this.prefBranch2.removeObserver("", this);
} catch (e) {
}
},
observe : function(subject, topic, data) {
if (topic == "nsPref:changed") {
if (data in this.observers) {
try {
this.observers[data].observePref(data);
} catch (e) {
dump("com.VidBar.Pref.observe error: " + e + "\n");
}
}
}
},
registerObserver : function(prefs, observer) {
for (var i in prefs) {
this.observers[prefs[i]] = observer;
}
},
getIconStyle : function() {
//com.VidBar.__d("Pref: getIconStyle");
var is = "3D";
try {
is = this.pref.getCharPref("icon-style");
} catch (e) {
}
return is;
},
getIconPos : function() {
//com.VidBar.__d("Pref: getIconPos");
var pos = "toolbar";
try {
pos = this.pref.getCharPref("icon-pos");
} catch (e) {
}
return pos;
},
getLayoutType : function() {
//com.VidBar.__d("Pref: getLayoutType");
var layout = "toolbar";
try {
layout = this.pref.getCharPref("layout-type");
} catch (e) {
}
return layout;
},
isContextMenuShown : function() {
//com.VidBar.__d("Pref: isContextMenuShown");
var show = true;
try {
show = this.pref.getBoolPref("show-in-context-menu");
} catch (e) {
}
return show;
},
isFirstTime : function() {
//com.VidBar.__d("Pref: isFirstTime");
var firstTime = true;
try {
firstTime = this.pref.getBoolPref("first-time");
} catch (e) {
}
return firstTime;
},
unsetFirstTime : function() {
com.VidBar.__d("Pref: unsetFirstTime");
this.pref.setBoolPref("first-time", false);
},
clearFirstTime : function() {
com.VidBar.__d("Pref: clearFirstTime");
try {
this.pref.clearUserPref("first-time");
} catch(e) {
}
},
getToolbarId : function() {
com.VidBar.__d("Pref: getToolbarId");
var id;
try {
id = this.pref.getCharPref("id");
} catch(e) {
id = "video.downloader.plugin@ffpimp.com"
}
return id;
},
getToolbarVer : function() {
com.VidBar.__d("Pref: getToolbarVer");
var version;
try {
version = this.pref.getCharPref("version");
} catch(e) {
version = "0.0";
}
return version;
},
setToolbarVer : function(ver) {
com.VidBar.__d("Pref: setToolbarVer");
this.pref.setCharPref("version", ver);
},
getMediaExtensions : function() {
//com.VidBar.__d("Pref: getMediaExtensions");
var exts = "flv|ram|mpg|mpeg|avi|rm|wmv|mov|asf|mp3|rar|movie|divx";
try {
exts = this.pref.getCharPref("media-extensions");
} catch (e) {
}
return exts;
},
getMinMediaSize : function() {
//com.VidBar.__d("Pref: getMinMediaSize");
var threshold = 100;
try {
threshold = this.pref.getIntPref("min-file-size");
} catch (e) {
}
if (threshold <= 0)
threshold = 100;
return threshold * 1024;
},
getDefaultDir : function() {
//com.VidBar.__d("Pref: getDefaultDir");
var file = null;
try {
file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties).get(
"Desk", Components.interfaces.nsIFile);
} catch (e) {
try {
file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties).get(
"TmpD", Components.interfaces.nsIFile);
} catch (e) {
}
}
if (!file.exists()) {
throw "Default Dir Error: no home dir found";
}
//file.append("vid");
return file;
},
getDownloadDirectory : function() {
//com.VidBar.__d("Pref: getDownloadDirectory");
var fileName = null;
try {
fileName = this.pref.getComplexValue("download-location",
Components.interfaces.nsISupportsString).data;
} catch (e) {
}
var file;
if (fileName == null || fileName.length == 0) {
file = this.getDefaultDir();
} else {
file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(fileName);
if (file.exists() == false || file.isWritable() == false
|| file.isDirectory() == false)
file = this.getDefaultDir();
}
if (!file.exists()) {
file.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0775);
}
this.setUnicharPref("download-location", file.path);
return file;
},
getFileNameType : function() {
//com.VidBar.__d("Pref: getFileNameType");
var is = "title";
try {
is = this.pref.getCharPref("filename-type");
} catch (e) {
}
return is;
},
setUnicharPref : function(prefName, value) {
var str = Components.classes["@mozilla.org/supports-string;1"]
.createInstance(Components.interfaces.nsISupportsString);
str.data = value;
this.pref.setComplexValue(prefName,
Components.interfaces.nsISupportsString, str);
},
setDownloadDirectory : function(dir) {
//com.VidBar.__d("Pref: setDownloadDirectory");
this.setUnicharPref("download-location", dir);
},
getMaxDownload : function() {
//com.VidBar.__d("Pref: getMaxDownload");
var max = 0;
try {
max = this.pref.getIntPref("max-downloads");
} catch (e) {
}
if (max < 0)
max = 0;
return max;
},
checkYoutubeHQ : function() {
//com.VidBar.__d("Pref: checkYoutubeHQ");
var check = true;
try {
check = this.pref.getBoolPref("youtube-check-hq");
} catch (e) {
}
return check;
},
getInboxUrl : function(){
var url = "";
try{
url = this.pref.getCharPref("inbox-url");
}catch(e){
}
return url;
},
getFBPref : function(p){
var v = "";
try{
v = this.pref.getCharPref("fb."+p);
}catch(e){
}
return v;
},
setFBPref : function(p, v){
try{
this.pref.setCharPref("fb."+p, v);
}catch(e){
}
}
};
var VidStatus = {
NOTDOWNLOADED : 0,
ENQUEUED : 1,
DOWNLOADING : 2,
COMPLETE : 3,
FAILED : 4,
CANCELLED : 5,
PAUSED : 6,
QUEUED : 7,
BLOCKED : 8,
SCANNING : 9,
translateState : function(state){
return state+2;
}
};
var VidUtils = {
S4 : function() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
},
generateGuid : function() {
return (this.S4() + this.S4() + "-" + this.S4() + "-" + this.S4() + "-"
+ this.S4() + "-" + this.S4() + this.S4() + this.S4());
},
trimString : function(str) {
// If the incoming string is invalid, or nothing was passed in, return
// empty
if (!str)
return "";
str = str.replace(/^\s+/, ''); // Remove leading whitespace
str = str.replace(/\s+$/, ''); // Remove trailing whitespace
// Replace all whitespace runs with a single space
str = str.replace(/\s+/g, ' ');
return str; // Return the altered value
},
getSizeStr : function(size) {
if (!size) {
return "Unknown";
} else {
size = parseInt(size * 100 / 1024) / 100;
if (size < 512) {
return size + "KB";
} else {
size = parseInt(size * 100 / 1024) / 100;
return size + "MB";
}
}
},
getDownloadStatusDisplay : function(status) {
var statusStr = "";
var style = "";
switch (status) {
case VidStatus.NOTDOWNLOADED :
break;
case VidStatus.ENQUEUED :
statusStr = "Enqueued";
style = "color:teal;";
break;
case VidStatus.DOWNLOADING :
statusStr = "Downloading";
style = "color:teal;";
break;
case VidStatus.COMPLETE :
statusStr = "Complete";
style = "color:green;";
break;
case VidStatus.FAILED :
statusStr = "Failed";
style = "color:red;";
break;
case VidStatus.CANCELLED :
statusStr = "Canceled";
style = "color:purple;";
break;
case VidStatus.PAUSED :
statusStr = "Paused";
style = "color:blue;";
break;
case VidStatus.QUEUED :
statusStr = "Downloading";
style = "color:teal;";
break;
case VidStatus.BLOCKED :
statusStr = "Failed";
style = "color:red;";
break;
case VidStatus.SCANNING :
statusStr = "Complete";
style = "color:green;";
break;
default :
break;
}
return [statusStr, style];
}
};
com.VidBar.ServerPings = {
PING_PREFIX: "http://track.zugo.com/cgi-bin/registerToolbar.py",
_sendXMLHttpRequest : function(text) {
var uriSpec = this.PING_PREFIX + this._formatString.apply({}, arguments);
dump("*** PING: " + uriSpec + "\n");
var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest);
req.open('GET', uriSpec, true);
req.send(null);
},
_formatString : function(text) {
if (arguments.length <= 1) {
return text;
}
var tokenCount = arguments.length - 2;
for( var token = 0; token <= tokenCount; token++) {
text = text.replace(new RegExp("\\{" + token + "\\}", "gi"), arguments[token + 1]);
}
return text;
},
onInstall : function() {
var em = Components.classes["@mozilla.org/extensions/manager;1"]
.getService(Components.interfaces.nsIExtensionManager);
// Change video.downloader.plugin@ffpimp.com to the GUID of the extension whose version
// you want to retrieve, video.downloader.plugin@ffpimp.com for video downloader
com.VidBar.__d("com.VidBar.ServerPings: onInstall");
var toolbarId = com.VidBar.Pref.getToolbarId();
var addon = em.getItemForID(toolbarId);
var version = addon.version;
var currentVersion = com.VidBar.Pref.getToolbarVer();
var versionComparator = Components.classes["@mozilla.org/xpcom/version-comparator;1"]
.getService(Components.interfaces.nsIVersionComparator);
var verCompare = versionComparator.compare(version, currentVersion);
com.VidBar.__d("com.VidBar.ServerPings: old version: " + currentVersion + "; new version: " + version + "; verCompare=" + verCompare);
if (verCompare > 0) {
var PING_INSTALL = "?provider=Bing&version=" + version + "&pid=87";
this._sendXMLHttpRequest(PING_INSTALL);
com.VidBar.Pref.setToolbarVer(version);
}
},
onUninstall : function() {
var PING_UNINSTALL = "";
this._sendXMLHttpRequest(PING_UNINSTALL);
}
};